﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CodeTop.复习九月份
{
    //这个题与计算器2的区别是加了括号，加了括号之后-（a+b)=-a-b,括号有影响
    public class Solution224
    {
        public int Calculate1(string s)//判断条件太多太乱，使用官方思想
        {
            Stack<int> stack = new Stack<int>();//标记正负号
            int preNum = 0;
            int len = s.Length;
            int preSign = 1;//默认，这样第一个数字直接入栈
            stack.Push(preSign);
            int res = 0;
            //先处理乘除
            for (int i = 0; i < s.Length; i++)
            {
                char c = s[i];
                if (char.IsDigit(c))
                {
                    int curNum = c - '0';
                    preNum = preNum * 10 + curNum;
                }

                //不是数字不是空格，就是操作符。
                if (!char.IsDigit(c) && c != ' ')
                {
                    Console.WriteLine(c);
                    switch (c)
                    {
                        case '+':
                            preSign = stack.Peek();
                            break;

                        case '-':
                            preSign = -stack.Peek();//负数入栈
                            break;

                        case '(':
                            stack.Push(preSign);
                            break;

                        default://')'
                            stack.Pop();
                            break;
                    }
                    //更新符号

                    res += preSign * preNum;
                    preNum = 0;
                }
            }
            res += preSign * preNum;
            return res;
        }

        public int Calculate(string s)//判断条件太多太乱，使用官方思想
        {
            Stack<int> ops = new Stack<int>();//标记正负号
            int preNum = 0;
            int len = s.Length;
            int sign = 1;//默认，这样第一个数字直接入栈
            ops.Push(sign);
            int res = 0;
            //先处理乘除
            int i = 0;
            while (i < len)
            {
                if (s[i] == ' ')
                {
                    i++;
                }
                else if (s[i] == '+')
                {
                    sign = ops.Peek();//加号不起作用
                    i++;
                }
                else if (s[i] == '-')
                {
                    sign = -ops.Peek();//+-或者--
                    i++;
                }
                else if (s[i] == '(')
                {
                    ops.Push(sign);//只有括号前面的才入栈，括号里面的与括号前面的一起作用
                    i++;
                }
                else if (s[i] == ')')
                {
                    ops.Pop();//把括号前面的弹出去
                    i++;
                }
                else
                {
                    int num = 0;
                    while (i < len && char.IsDigit(s[i]))
                    {
                        num = num * 10 + s[i] - '0';
                        i++;
                    }
                    res += sign * num;
                }
            }
            return res;
        }
    }

    internal class Program224
    {
        private static void Main224(string[] args)
        {
            // string s = " 2-1 + 2 ";
            //string s = "1 + 1";
            // string s = "(1+(4+5+2)-3)+(6+8)";//23
            string s = "(1-(4+5+2)-3)+(6+8)";//23

            Solution224 Solution = new Solution224();
            int res = Solution.Calculate(s);
            Console.WriteLine(res);
        }
    }
}